import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import MainLayout from '@/layouts/MainLayout.vue'

// 路由表
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/welcome'
  },
  {
    path: '/welcome',
    name: 'Welcome',
    component: () => import('@/pages/welcome/Welcome.vue')
  },
  {
    path: '/projects',
    name: 'ProjectList',
    component: () => import('@/pages/projects/ProjectList.vue')
  },
  {
    path: '/account',
    name: 'Account',
    component: MainLayout,
    children: [
      {
        path: 'login',
        name: 'Login',
        component: () => import('@/pages/account/Login.vue')
      },
      {
        path: 'register',
        name: 'Register',
        component: () => import('@/pages/account/Register.vue')
      },
      {
        path: 'forgot-password',
        name: 'ForgotPassword',
        component: () => import('@/pages/account/ForgotPassword.vue')
      }
    ]
  },
  {
    path: '/workspace',
    name: 'Workspace',
    component: MainLayout,
    redirect: '/workspace/editor',
    children: [
      {
        path: 'editor',
        name: 'Editor',
        component: () => import('@/components/editor/EditorWorkspace.vue')
      },
      {
        path: 'settings',
        name: 'ProjectSettings',
        component: () => import('@/pages/workspace/ProjectSettings.vue')
      }
    ]
  },
  // 用户中心
  {
    path: '/user',
    component: MainLayout,
    children: [
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('@/pages/account/UserCenter.vue')
      },
      {
        path: 'creator',
        name: 'CreatorProfile',
        component: () => import('@/pages/account/CreatorCenter.vue')
      }
    ]
  },
  // 导出功能
  {
    path: '/export',
    name: 'Export',
    component: () => import('@/pages/workspace/Export.vue')
  },
  // 角色编辑模块
  {
    path: '/character',
    name: 'CharacterEditor',
    component: () => import('@/pages/character/CharacterEditor.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/expression',
    name: 'ExpressionEditor',
    component: () => import('@/pages/character/ExpressionEditor.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/action-editor',
    name: 'ActionEditor',
    component: () => import('@/pages/character/ActionEditor.vue'),
    meta: { requiresAuth: true }
  },
  // 场景制作模块
  {
    path: '/scene-editor',
    name: 'SceneEditor',
    component: () => import('@/pages/scene/SceneEditor.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/transition-editor',
    name: 'TransitionEditor',
    component: () => import('@/pages/scene/TransitionEditor.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/environment-editor',
    name: 'EnvironmentEditor',
    component: () => import('@/pages/scene/EnvironmentEditor.vue'),
    meta: { requiresAuth: true }
  },
  // 时间轴模块
  {
    path: '/timeline',
    name: 'Timeline',
    component: () => import('@/pages/timeline/Timeline.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/audio-editor',
    name: 'AudioEditor',
    component: () => import('@/pages/timeline/AudioEditor.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/subtitle-editor',
    name: 'SubtitleEditor',
    component: () => import('@/pages/timeline/SubtitleEditor.vue'),
    meta: { requiresAuth: true }
  },
  // AI生成模块
  {
    path: '/ai-text-generator',
    name: 'AITextGenerator',
    component: () => import('@/pages/ai/TextGenerator.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/ai-result-editor',
    name: 'AIResultEditor',
    component: () => import('@/pages/ai/ResultEditor.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/ai-training',
    name: 'AITraining',
    component: () => import('@/pages/ai/Training.vue'),
    meta: { requiresAuth: true, creatorOnly: true }
  },
  // 插件市场模块
  {
    path: '/market',
    name: 'Market',
    component: () => import('@/pages/market/Market.vue')
  },
  {
    path: '/asset-detail/:id',
    name: 'AssetDetail',
    component: () => import('@/pages/market/AssetDetail.vue')
  },
  {
    path: '/creator-upload',
    name: 'CreatorUpload',
    component: () => import('@/pages/market/CreatorUpload.vue'),
    meta: { requiresAuth: true, creatorOnly: true }
  },
  // 社区互动模块
  {
    path: '/community',
    name: 'Community',
    component: () => import('@/pages/community/Community.vue')
  },
  {
    path: '/work-detail/:id',
    name: 'WorkDetail',
    component: () => import('@/pages/community/WorkDetail.vue')
  },
  {
    path: '/user-profile/:id',
    name: 'UserProfile',
    component: () => import('@/pages/community/UserProfile.vue')
  },
  // 系统设置模块
  {
    path: '/settings',
    name: 'Settings',
    component: () => import('@/pages/settings/Settings.vue'),
    children: [
      {
        path: 'general',
        name: 'GeneralSettings',
        component: () => import('@/pages/settings/General.vue')
      },
      {
        path: 'performance',
        name: 'PerformanceSettings',
        component: () => import('@/pages/settings/Performance.vue')
      },
      {
        path: 'cloud',
        name: 'CloudSettings',
        component: () => import('@/pages/settings/Cloud.vue')
      },
      {
        path: 'electron',
        name: 'ElectronSettings',
        component: () => import('@/pages/settings/Electron.vue')
      }
    ]
  },
  // 帮助与支持模块
  {
    path: '/help',
    name: 'HelpCenter',
    component: () => import('@/pages/help/HelpCenter.vue')
  },
  {
    path: '/tutorials',
    name: 'Tutorials',
    component: () => import('@/pages/help/Tutorials.vue')
  },
  {
    path: '/feedback',
    name: 'Feedback',
    component: () => import('@/pages/help/Feedback.vue')
  },
  {
    path: '/faq',
    name: 'FAQ',
    component: () => import('@/pages/help/FAQ.vue')
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/pages/errors/NotFound.vue')
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 皮影快闪` : '皮影快闪'
  
  // 权限验证
  if (to.meta.requiresAuth) {
    // 检查是否已登录
    const isLoggedIn = localStorage.getItem('userToken')
    if (!isLoggedIn) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router 